package com.luo.chengrui.module.sa.service.datapermission;

import com.luo.chengrui.module.sa.service.datapermission.DataPermissionItemSubService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.luo.chengrui.module.sa.controller.admin.datapermission.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.datapermission.DataPermissionItemSubDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import com.luo.chengrui.module.sa.convert.datapermission.DataPermissionItemSubConvert;
import com.luo.chengrui.module.sa.dal.mysql.datapermission.DataPermissionItemSubMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.luo.chengrui.module.sa.enums.ErrorCodeConstants.*;;

/**
 * 数据权限项明细，代码中用来配置SaDataPermission注解中变量permissionCode的取值。 Service 实现类
 *
 *
 *
 * @author 诚睿
 */
@Service
@Validated
public class DataPermissionItemSubServiceImpl implements DataPermissionItemSubService {

    @Resource
    private DataPermissionItemSubMapper dataPermissionItemSubMapper;

    @Override
    public String createDataPermissionItemSub(DataPermissionItemSubCreateReqVO createReqVO) {
        // 插入
        DataPermissionItemSubDO dataPermissionItemSub = DataPermissionItemSubConvert.INSTANCE.convert(createReqVO);
        validateSubCodeExists(dataPermissionItemSub.getId(), dataPermissionItemSub.getCode());
        dataPermissionItemSubMapper.insert(dataPermissionItemSub);
        // 返回
        return dataPermissionItemSub.getId();
    }

    @Override
    public void updateDataPermissionItemSub(DataPermissionItemSubUpdateReqVO updateReqVO) {
        // 校验存在
        validateDataPermissionItemSubExists(updateReqVO.getId());
        validateSubCodeExists(updateReqVO.getId(), updateReqVO.getCode());
        // 更新
        DataPermissionItemSubDO updateObj = DataPermissionItemSubConvert.INSTANCE.convert(updateReqVO);
        dataPermissionItemSubMapper.updateById(updateObj);
    }

    @Override
    public void deleteDataPermissionItemSub(String id) {
        // 校验存在
        validateDataPermissionItemSubExists(id);
        // 删除
        dataPermissionItemSubMapper.deleteById(id);
    }

    private void validateDataPermissionItemSubExists(String id) {
        if (dataPermissionItemSubMapper.selectById(id) == null) {
            throw exception(DATA_PERMISSION_ITEM_SUB_NOT_EXISTS);
        }
    }

    private void validateSubCodeExists(String id, String code) {
        DataPermissionItemSubDO subObj = dataPermissionItemSubMapper.selectOne(DataPermissionItemSubDO::getCode, code);
        if (subObj == null) {
            return;
        }
        if (id == null) {
            throw exception(DATA_PERMISSION_ITEM_SUB_CODE_EXISTS);
        }
        if (!Objects.equals(id, subObj.getId())) {
            throw exception(DATA_PERMISSION_ITEM_SUB_CODE_EXISTS);
        }
    }

    @Override
    public DataPermissionItemSubDO getDataPermissionItemSub(String id) {
        return dataPermissionItemSubMapper.selectById(id);
    }

    @Override
    public List<DataPermissionItemSubDO> getDataPermissionItemSubList(Collection<String> ids) {
        return dataPermissionItemSubMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DataPermissionItemSubDO> getDataPermissionItemSubPage(DataPermissionItemSubPageReqVO pageReqVO) {
        return dataPermissionItemSubMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DataPermissionItemSubDO> getDataPermissionItemSubList(DataPermissionItemSubExportReqVO exportReqVO) {
        return dataPermissionItemSubMapper.selectList(exportReqVO);
    }

}
